layui.use(['jquery','layer','table','form'],function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;

    var couponName = '';
    var give = table.render({
        elem:"#table",
        url:"/coupon/getCouponGive?couponName="+couponName,
        toolbar:"#tool",
        page:true,
        limit:7,
        limits:[7,14,21,28],
        cols:[
            [
                {type:"checkbox",align:"center"},
                {type:"numbers",title:"序号",align:"center"},
                {field:"couponName",title:"优惠券名称",align:"center"},
                {field:"couponPrice",title:"优惠券面值",align:"center",templet:function (data) {
                        return "￥"+data.couponPrice;
                    }},
                {field:"type",title:"优惠券类型",align:"center",templet:function (data) {
                        if (data.type == 2)
                            return "商品";
                        return '视频';
                    }},
                {field:"",title:"有效期",align:"center",width:200,templet:function (data) {
                        return "开始:"+data.startDate+
                            "结束"+data.endDate;
                    }},
            ]
        ],
    });


    /* 模糊查询开始 */
    form.on("submit(sub)",function () {
        var couponName = $("#couponName").val();
        give.reload({
            url:"/coupon/getCouponGive?couponName="+couponName,
        });
        return false;
    });
    /* 模糊查询结束 */

    /* 确定 */
    //取消
    $("#queding").click(function () {
        //监听表中数据
        var checkStatus = table.checkStatus("table").data;
        if(checkStatus.length > 0){
            checkStatus.forEach(function (data) {
                var userId = $("#userId").val();
                var json = {
                    userId:userId,
                    couponId:data.couponId,
                    remarks:2
                };
                $.ajax({
                    url:"/coupon/addGiveCoupon",
                    data:json,
                    dataType:"json",
                    Type:"post",
                    success:function (data) {
                        if (data){
                            layer.msg("赠送成功");
                            var index  = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            parent.location.reload();
                        }else{
                            layer.msg("赠送失败");
                        }

                    }
                })
            })
        }
    });
    /* 确定 */

    /* 取消 */
    $("#quxiao").click(function () {
        var index  = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    });
    /* 取消 */
});